#!/usr/bin/env python

try:
	import os
	os_loaded = True
except ImportError:
	os_loaded = False

try:
	import sys
	sys_loaded = True
except ImportError:
	sys_loaded = False

try:
	import pwd
	import grp
	pwd_loaded = True
except ImportError:
	pwd_loaded = False

try:
	import gtk
	gtk_loaded = True
except ImportError:
	gtk_loaded = False

try:
	import gobject
	gobject_loaded = True
except ImportError:
	gobject_loaded = False

try:
	import socket
	socket_loaded = True
except ImportError:
	socket_loaded = False	

try:
	import dbus
	import dbus
	import dbus.bus
	import dbus.exceptions
	import dbus.mainloop.glib
	dbus_loaded = True
except ImportError:
	dbus_loaded = False

try:
	import logging
	logging_loaded = True
except ImportError:
	logging_loaded = False

try:
	import webkit
	webkit_loaded = True
except ImportError:
	webkit_loaded = False

try:
	import Image
	pimage_loaded = True
except ImportError:
	pimage_loaded = False

try:
	import sqlite
	sqlite_loaded = True
except ImportError:
	sqlite_loaded = False
	
try:
	import cairo
	cairo_loaded = True
except ImportError:
	cairo_loaded = False

class LogWin(gtk.Window):

	url = 'file:///usr/share/webkit-logon/desktop.html'
	#url = 'http://2advanced.com/#/home'
	#url = 'http://www.mygoya.de/us'
	#url = 'http://www.glidedigital.com/'
	#url = 'https://www.icloud.com/'
	#url = 'http://www.yoodos.com/'
	#url = ''
	#url = ''
	#url = ''
	#url = ''
	#url = ''
	#url = 'http://www.netvibes.com/privatepage/1#Internet'

	def transparent_expose(self, widget, event):
		cr = widget.window.cairo_create()
		cr.set_operator(cairo.OPERATOR_CLEAR)
		region = gtk.gdk.region_rectangle(event.area)
		cr.region(region)
		cr.fill()
	
	def no_delete(self,a,b):
		return True

	def __init__(self, args):
		gtk.gdk.threads_init()
		gtk.remove_log_handlers()
		gtk.Window.__init__(self)
		self.fullscreen()
		self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
		self.set_keep_below(True)
		self.set_decorated(False)
		self.set_skip_taskbar_hint(True)
		self.set_skip_pager_hint(True)
		self.stick()
		self.screen = self.get_screen()
		self.rgba = self.screen.get_rgba_colormap()
		self.set_colormap(self.rgba)
		self.set_app_paintable(True)
		self.connect("expose-event", self.transparent_expose)
		
		self.view = webkit.WebView()
		self.add(self.view)
		settings = webkit.WebSettings()
		settings.set_property("enable-plugins", False)
		settings.set_property('enable-file-access-from-file-uris', 1)
		settings.set_property('enable-default-context-menu', False)
		self.view.set_settings(settings)
		self.view.connect("console-message", self.console)
		self.view.connect('load-finished', self._finished_loading)
		self.view.connect('title-changed', self.title_changed)
		self.view.show_all()
		self.show_all()
		self.fullscreen()
		self.view.open(self.url)
		pass

	def restart_computer(self):
		os.execl('/sbin/shutdown',' -r now')

	def halt_computer(self):
		os.execl('/sbin/shutdown',' -h now')
	
	def hibernate_computer(self):
		os.execl('','')

	def exit(self,a=False,b=False):
		self.view.hide()
		self.hide()
		self.unfullscreen()
		mainloop.quit()
		gtk.main_quit()
		sys.exit(0)
		pass

	def _finished_loading(self, view, frame):
		pass

	def get_html(self):
		self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
		html = self.get_main_frame().get_title()
		self.execute_script('document.title=oldtitle;')
		return html

	def console(self, webview, message, lineno, fileurl):
		self.log.e('got script or css or html error:')
		self.log.e('script: ' + fileurl +', line: '+ str(lineno) + "\n" + message + "\n")
		webview.stop_emission("console-message")
		pass

	def title_changed(self, widget, frame, title):
		self.log.i('got result from greeter')
		args = title.split(':')
		cmd = args[0]

		if(cmd=='comprestart'):
			if(not(self.test)):
				self.restart_computer()
			else:
				self.exit()

		if(cmd=='comphalt'):
			if(not(self.test)):
				self.halt_computer()
			else:
				self.exit()
		
		#if(cmd=='comphibernate'):
			#if(not(self.test)):
				#self.halt_computer()
			#else:
				#self.exit()

		if(cmd=='exit'):
			self.udata.close()
			self.exit()
			pass

### MAIN ###########################################################################
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
mainloop = gobject.MainLoop()
win = LogWin(sys.argv)
gtk.main()
####################################################################################
